import Vue from 'vue'

import App from './App.vue'

import '@/api/mock.js'

import store from '@/views/backend/store'
//引入element——Ui
import '@/assets/less/index.less'

import http from 'axios'

import ElementUI from 'element-ui';

import 'element-ui/lib/theme-chalk/index.css';

import router from "./routes/index"
//引入富文本编辑器
import mavonEditor from 'mavon-editor'

import {delToken, getToken} from "@/request/auth.js"

import 'mavon-editor/dist/css/index.css'
import $ from 'jquery';
window.jQuery = $;
window.$ = $;
Vue.use(mavonEditor)
import { MessageBox, Message } from 'element-ui'


Vue.use(ElementUI);
Vue.prototype.$http = http
Vue.prototype.$confirm = MessageBox.confirm
Vue.prototype.$message = Message
Vue.config.productionTip = false
window.onload = function () {
  let lastTime = localStorage.getItem("lastTime");
  const interval = 3 * 10000;
  // 如果没有上一次离开的时间或者时间间隔大于3s，就清除token
  if (!lastTime || new Date().getTime() - lastTime > interval) {
      delToken();
      console.log("清除token")
      window.location.reload();
  }else{
      console.log("时间过短不清除token")
  }
};
window.onunload = function () {
  localStorage.setItem("lastTime", new Date().getTime());
};
router.beforeEach((to, form, next) => {
  let token = getToken();
  if (!token && to.name !== 'login') {
    next({ name: 'login' })
  }
  else if (token && to.name === 'login' && this.$state.tab.Menu==null) {
  this.$state.Dispatch('incrementAsync()')
    next(
      { path: '/home' }
    )
  } else {
    next()
  }
})
new Vue({
  store,
  router,
  render: h => h(App),
}).$mount('#app')
